package ink.xiaobaibai.common;

import com.alibaba.fastjson.JSON;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import org.springframework.stereotype.Component;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @description:
 * @author: 小白白
 * @create: 2021-06-03
 **/

@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes({TimedTask.class})
@Component
public class TimedTaskHandler implements TypeHandler<TimedTask> {

    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, TimedTask timedTask, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, JSON.toJSONString(timedTask));
    }

    @Override
    public TimedTask getResult(ResultSet resultSet, String s) throws SQLException {
        return JSON.parseObject(resultSet.getString(s), TimedTask.class);
    }

    @Override
    public TimedTask getResult(ResultSet resultSet, int i) throws SQLException {
        return JSON.parseObject(resultSet.getString(i), TimedTask.class);
    }

    @Override
    public TimedTask getResult(CallableStatement callableStatement, int i) throws SQLException {
        return JSON.parseObject(callableStatement.getString(i), TimedTask.class);
    }

}
